"""
-------------------------------
- File_Name: users.py
- Date: 2025/2/23
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: 用户模型
-------------------------------
"""


from app.core.security import get_password_hash
from app.models.utils import GenderType, StatusType
from pydantic import BaseModel, Field,field_validator
from config import get_api_environment_var
from app.utils.common import to_camel_case

api_environment = get_api_environment_var()


class SystemUserBase(BaseModel):
    """系统管理-用户列表"""
    """编号"""
    user_code: str | None = Field(None)
    """用户名"""
    user_name: str | None = Field(None)
    """昵称"""
    nick_name: str | None = Field(None)
    """邮箱"""
    user_email: str | None = Field(None)
    """性别，"1": "男" , "2": "女\""""
    user_gender: str = Field(GenderType.male)
    """手机号码"""
    user_phone: str | None = Field(None)
    """角色集合- user_role表存储"""
    user_roles: list[str] | None = Field(None)
    """启用状态，"1": "启用" , "2": "停用\""""
    status: str = Field(StatusType.disable)
    """业主ID"""
    yez_id: str | None = Field("")

    class Config:
        alias_generator = to_camel_case
        populate_by_name = True




class SystemUserCreate(SystemUserBase):
    """创建系统管理-用户创建"""
    """密码"""
    password: str | None  = ''
    """创建者"""
    create_by: str | None = None

    class Config:
        # 使用别名生成器, 自动将下划线命名转换为驼峰命名作为别名
        alias_generator = to_camel_case
        # 允许通过字段名或别名赋值
        populate_by_name = True

    # 反序列化时验证
    @field_validator("password")
    @classmethod
    def check_password(cls, value):
        if value == "":
            # 使用默认密码
            return get_password_hash(api_environment.default_password)
        return get_password_hash(value)

class SystemUserUpdate(SystemUserBase):
    """更新系统管理-用户更新"""

    """更新者"""
    update_by: str | None = None

    class Config:
        alias_generator = to_camel_case
        populate_by_name = True


class SystemUserOut(SystemUserBase):
    """返回系统用户信息"""
    """唯一标识"""
    id: int
    """创建时间"""
    create_time: str | None = Field(None)
    """创建者"""
    create_by: str | None = Field(None)
    """更新时间"""
    update_time: str | None = Field(None)
    """更新者"""
    update_by: str | None = Field(None)
    """在线状态"""
    online: str | None = Field(None)
    """最后登录时间"""
    last_login: str | None = Field(None)
    """角色集合"""
    userRoles: list[str] | None = Field(None)

    class Config:
        alias_generator = to_camel_case
        populate_by_name = True

class SystemUserUpdatePwd(BaseModel):
    """用户登录密码更新"""
    """用户原密码"""
    old_password: str
    """用户新密码"""
    new_password: str

    class Config:
        alias_generator = to_camel_case
        populate_by_name = True

class SystemUserUpdateNewPwd(BaseModel):
    """更新新密码"""
    password: str

